package jnpf.onlinedev.util.onlineDevUtil;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jnpf.base.ActionResult;
import jnpf.base.entity.DictionaryDataEntity;
import jnpf.base.entity.ProvinceEntity;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.service.*;
import jnpf.base.util.VisualUtils;
import jnpf.database.entity.DbLinkEntity;
import jnpf.database.util.JdbcUtil;
import jnpf.exception.DataException;
import jnpf.model.visiual.FormDataModel;
import jnpf.model.visiual.JnpfKeyConsts;
import jnpf.model.visiual.TableModel;
import jnpf.model.visiual.fields.FieLdsModel;
import jnpf.model.visiual.fields.props.PropsBeanModel;
import jnpf.onlinedev.model.OnlineDevEnum.CacheKeyEnum;
import jnpf.onlinedev.model.OnlineDevEnum.OnlineDataTypeEnum;
import jnpf.onlinedev.model.OnlineDevListModel.OnlineDevListDataVO;
import jnpf.onlinedev.model.OnlineDevListModel.OnlineFieldsModel;
import jnpf.onlinedev.model.VisualdevModelDataInfoVO;
import jnpf.onlinedev.service.VisualdevModelDataService;
import jnpf.permission.service.OrganizeService;
import jnpf.permission.service.PositionService;
import jnpf.permission.service.UserService;
import jnpf.util.DateUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RedisUtil;
import jnpf.util.StringUtil;
import jnpf.util.context.SpringContext;
import lombok.Cleanup;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 在线详情编辑工具类
 *
 * @author JNPF开发平台组
 * @version V3.2
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date  2021/10/27
 */
public class OnlineDevInfoUtils {
	private static RedisUtil redisUtil;
	private static DictionaryDataService dictionaryDataService;
	private static UserService userService;
	private static PositionService positionService;
	private static ProvinceService provinceService;
	private static OrganizeService organizeService;
	private static VisualdevService visualdevService;
	private static VisualdevModelDataService visualdevModelDataService;
	private static DataInterfaceService dataInterfaceService;
	private static DblinkService dblinkService;

	public static void init() {
		visualdevModelDataService = SpringContext.getBean(VisualdevModelDataService.class);
		dictionaryDataService = SpringContext.getBean(DictionaryDataService.class);
		userService = SpringContext.getBean(UserService.class);
		positionService = SpringContext.getBean(PositionService.class);
		redisUtil = SpringContext.getBean(RedisUtil.class);
		provinceService = SpringContext.getBean(ProvinceService.class);
		organizeService = SpringContext.getBean(OrganizeService.class);
		visualdevService = SpringContext.getBean(VisualdevService.class);
		dataInterfaceService = SpringContext.getBean(DataInterfaceService.class);
		dblinkService = SpringContext.getBean(DblinkService.class);
	}

	/**
	 * @param modelList 模型
	 * @param dataMap   数据
	 * @return
	 */
	public static Map<String, Object> swapTableDataInfo(List<FieLdsModel> modelList, Map<String, Object> dataMap, String visualDevId) throws SQLException, ParseException, DataException, IOException {
		init();
		//从缓存中取出数据
		if (!redisUtil.exists(visualDevId)) {
			return dataMap;
		}

		//组织
		Map<Object, Object> orgMap = redisUtil.getMap(visualDevId + CacheKeyEnum.ORG.getName());
		//岗位
		Map<Object, Object> posMap = redisUtil.getMap(visualDevId + CacheKeyEnum.POS.getName());
		//人员
		Map<Object, Object> userMap = redisUtil.getMap(visualDevId + CacheKeyEnum.USER.getName());
		//省市区
		Map<Object, Object> proMap = redisUtil.getMap(visualDevId + CacheKeyEnum.PRO.getName());
		init();
		for (FieLdsModel swapDataVo : modelList) {
			String jnpfKey = swapDataVo.getConfig().getJnpfKey();
			String vModel = swapDataVo.getVModel();
			String dataType = String.valueOf(swapDataVo.getConfig().getDataType());
			if (StringUtil.isEmpty(String.valueOf(dataMap.get(vModel))) || dataMap.get(vModel) == null) {
				continue;
			}
			if (String.valueOf(dataMap.get(vModel)).equals("[]") || String.valueOf(dataMap.get(vModel)).equals("null")) {
				dataMap.put(vModel, null);
			} else {
				switch (jnpfKey) {
					//公司组件
					case JnpfKeyConsts.COMSELECT:
						//部门组件
					case JnpfKeyConsts.DEPSELECT:
						//所属部门
					case JnpfKeyConsts.CURRDEPT:
						//所属公司
					case JnpfKeyConsts.CURRORGANIZE:
						dataMap.put(vModel, getDataInMethod(orgMap, dataMap.get(vModel)));
						break;

					//岗位组件
					case JnpfKeyConsts.POSSELECT:
						//所属岗位
					case JnpfKeyConsts.CURRPOSITION:
						dataMap.put(vModel, getDataInMethod(posMap, dataMap.get(vModel)));
						break;

					//用户组件
					case JnpfKeyConsts.USERSELECT:
						//创建用户
					case JnpfKeyConsts.CREATEUSER:
						//修改用户
					case JnpfKeyConsts.MODIFYUSER:
						dataMap.put(vModel, getDataInMethod(userMap, dataMap.get(vModel)));
						break;

					//省市区联动
					case JnpfKeyConsts.ADDRESS:
						List<String> proDataS = JsonUtil.getJsonToList(String.valueOf(dataMap.get(vModel)), String.class);
						proDataS = proDataS.stream().map(pro -> String.valueOf(proMap.get(pro))).collect(Collectors.toList());
						dataMap.put(vModel, String.join(",", proDataS));
						break;
					//开关
					case JnpfKeyConsts.SWITCH:
						String switchValue = String.valueOf(dataMap.get(vModel)).equals("1") ? "开" : "关";
						dataMap.put(vModel, switchValue);
						break;
					//级联
					case JnpfKeyConsts.CASCADER:
						String redisKey;
						if (dataType.equals(OnlineDataTypeEnum.STATIC.getType())) {
							redisKey = vModel + "_" + OnlineDataTypeEnum.STATIC.getType();
						} else if (dataType.equals(OnlineDataTypeEnum.DYNAMIC.getType())) {
							redisKey = vModel + "_" + OnlineDataTypeEnum.DYNAMIC.getType();
						} else {
							redisKey = vModel + "_" + OnlineDataTypeEnum.DICTIONARY.getType();
						}
						Map<Object, Object> cascaderMap = redisUtil.getMap(redisKey);
						String value = String.valueOf(dataMap.get(vModel));
						String multiple = swapDataVo.getMultiple();
						Boolean isMultiple = StringUtil.isEmpty(multiple) || multiple.equals("false") ? false : true;
						if (isMultiple) {
							String[][] data = JsonUtil.getJsonToBean(value,String[][].class);
							String casDataS="";
							StringBuilder cascaderData = new StringBuilder();
							for (String[] casData :data){
								for (String s : casData){
									casDataS= String.valueOf(cascaderMap.get(s)) ;
									cascaderData.append(casDataS+",");
								}
							}
							cascaderData=cascaderData.deleteCharAt(cascaderData.length()-1);
							dataMap.put(vModel,cascaderData);
						} else {
							dataMap.put(vModel, getDataInMethod(cascaderMap, dataMap.get(vModel)));
						}
						break;
					case JnpfKeyConsts.CHECKBOX:
						String checkBox;
						if (dataType.equals(OnlineDataTypeEnum.STATIC.getType())) {
							checkBox = vModel + "_" + OnlineDataTypeEnum.STATIC.getType();
						} else if (dataType.equals(OnlineDataTypeEnum.DYNAMIC.getType())) {
							checkBox = vModel + "_" + OnlineDataTypeEnum.DYNAMIC.getType();
						} else {
							checkBox = vModel + "_" + OnlineDataTypeEnum.DICTIONARY.getType();
						}
						Map<Object, Object> checkboxMap = redisUtil.getMap(checkBox);
						dataMap.put(vModel, getDataInMethod(checkboxMap, dataMap.get(vModel)));
						break;
					case JnpfKeyConsts.RELATIONFORM:
						//取关联表单数据
						VisualdevEntity entity = visualdevService.getInfo(swapDataVo.getModelId());
						DbLinkEntity linkEntity = dblinkService.getInfo((entity.getDbLinkId()));
						@Cleanup Connection connection = VisualUtils.getDataConn(linkEntity);
						List<TableModel> tableModels = JsonUtil.getJsonToList(entity.getTables(), TableModel.class);
						Map<String, Object> formDataMap = new HashMap<>(16);
						if (tableModels.size() > 0) {
							//取关联主表
							TableModel tableModel = tableModels.stream().filter(table -> table.getTypeId().equals("1")).findFirst().orElse(null);
							String relationField = swapDataVo.getRelationField();
							String relationMainTable = tableModel.getTable();
							//取关联主键
							String pkeyName = VisualUtils.getpKey(connection, relationMainTable);
							String sql = "select " + relationField + " from " + relationMainTable + " where " + pkeyName + "='" + dataMap.get(vModel) + "'";
							formDataMap = JdbcUtil.queryOne(connection, sql);
						} else {
							VisualdevModelDataInfoVO vo = visualdevModelDataService.infoDataChange(String.valueOf(dataMap.get(vModel)), entity);
							if (vo != null) {
								formDataMap = JsonUtil.stringToMap(vo.getData());
							}
						}
						if (formDataMap.size() > 0) {
							OnlineDevListDataVO devListDataVo = new OnlineDevListDataVO();
							formDataMap = OnlinePublicUtils.mapKeyToLower(formDataMap);
							devListDataVo.setData(formDataMap);
							List<OnlineDevListDataVO> formList = new ArrayList<>();
							formList.add(devListDataVo);

							FormDataModel formDataModel = JsonUtil.getJsonToBean(entity.getFormData(), FormDataModel.class);
							List<OnlineFieldsModel> formDataVolist = JsonUtil.getJsonToList(formDataModel.getFields(), OnlineFieldsModel.class);
							List<OnlineDevListDataVO> formSwapList = OnlineDevListUtils.getSwapList(formList, formDataVolist, swapDataVo.getModelId());
							OnlineDevListDataVO dataVo = formSwapList.stream().findFirst().orElse(null);
							dataMap.put(vModel, dataVo.getData().get(swapDataVo.getRelationField().toLowerCase()));
						}
						break;
					case JnpfKeyConsts.POPUPSELECT:
						Object data = dataInterfaceService.infoToId(swapDataVo.getInterfaceId()).getData();
						List<Map<String, Object>> mapList = (List<Map<String, Object>>) data;
						mapList.stream().filter(map -> map.get(swapDataVo.getPropsValue()).equals(dataMap.get(vModel)))
								.forEach(modelMap -> dataMap.put(vModel, modelMap.get(swapDataVo.getColumnOptions().get(0).getValue())));
						break;
					case JnpfKeyConsts.MODIFYTIME:
					case JnpfKeyConsts.CREATETIME:
					case JnpfKeyConsts.DATE:
						//判断是否为时间戳格式
						String format;
						String dateData = String.valueOf(dataMap.get(vModel));
						String dateSwapInfo = swapDataVo.getFormat() != null ? swapDataVo.getFormat() : swapDataVo.getType() != null && swapDataVo.getType().equals(JnpfKeyConsts.DATE) ? "yyyy-MM-dd" : "yyyy-MM-dd HH:mm:ss";
						if (!dateData.contains("-") && !dateData.contains(":") && dateData.length() > 10) {
							DateTimeFormatter ftf = DateTimeFormatter.ofPattern(dateSwapInfo);
							format = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli((Long) dataMap.get(vModel)), ZoneId.of("+8")));
						} else {
							format = dateData;
						}
						if (format.contains(".")) {
							format = format.substring(0, format.lastIndexOf("."));
						}
						SimpleDateFormat sdf = new SimpleDateFormat(dateSwapInfo);
						Date date = sdf.parse(format);
						String outTime = sdf.format(sdf.parse(DateUtil.dateFormat(date)));
						dataMap.put(vModel, outTime);
						break;
					case JnpfKeyConsts.UPLOADFZ:
					case JnpfKeyConsts.UPLOADIMG:
						List<Map<String, Object>> fileList = JsonUtil.getJsonToListMap(String.valueOf(dataMap.get(vModel)));
						dataMap.put(vModel,fileList);
						break;
					default:
						break;
				}
				//转换数据接口的数据
				if (dataType != null) {
					if (!jnpfKey.equals(JnpfKeyConsts.CASCADER) && !jnpfKey.equals(JnpfKeyConsts.CHECKBOX)) {
						//静态数据
						if (dataType.equals(OnlineDataTypeEnum.STATIC.getType())) {
							String redisKey = vModel + "_" + OnlineDataTypeEnum.STATIC.getType();
							Map<Object, Object> staticMap = redisUtil.getMap(redisKey);

							dataMap.put(vModel, getDataInMethod(staticMap, dataMap.get(vModel)));

							//远端数据
						} else if (dataType.equals(OnlineDataTypeEnum.DYNAMIC.getType())) {
							String redisKey = vModel + "_" + OnlineDataTypeEnum.DYNAMIC.getType();
							Map<Object, Object> dynamicMap = redisUtil.getMap(redisKey);
							dataMap.put(vModel, getDataInMethod(dynamicMap, dataMap.get(vModel)));

							//数据字典
						} else if (dataType.equals(OnlineDataTypeEnum.DICTIONARY.getType())) {
							String redisKey = vModel + "_" + OnlineDataTypeEnum.DICTIONARY.getType();
							Map<Object, Object> dictionaryMap = redisUtil.getMap(redisKey);
							dataMap.put(vModel, getDataInMethod(dictionaryMap, dataMap.get(vModel)));
						}
					}
				}
			}
		}
		return dataMap;
	}

	public static Object getDataInMethod(Map<Object, Object> redisMap, Object modelData) {
		String s2;
		if (redisMap.size() == 0) {
			return modelData;
		}
		if (modelData instanceof ArrayList) {
			s2 = ((ArrayList) modelData).stream().map(s -> String.valueOf(redisMap.get(s))).collect(Collectors.joining(",")).toString();
		} else {
			if (String.valueOf(modelData).contains("[")) {
				List<String> modelDataList = JsonUtil.getJsonToList(String.valueOf(modelData), String.class);
				modelDataList = modelDataList.stream().map(s -> String.valueOf(redisMap.get(s))).collect(Collectors.toList());
				s2 = String.join(",", modelDataList);
			} else {
				String[] modelDatas = String.valueOf(modelData).split(",");
				StringBuilder dynamicData = new StringBuilder();
				for (int i = 0; i < modelDatas.length; i++) {
					modelDatas[i] = String.valueOf(redisMap.get(modelDatas[i]));
					dynamicData.append(modelDatas[i] + ",");
				}
				s2 = dynamicData.deleteCharAt(dynamicData.length() - 1).toString();
			}
		}
		return StringUtil.isEmpty(s2) ? modelData.toString() : s2;
	}

	/**
	 * 子表数据转换
	 *
	 * @param modelList
	 * @param dataMap
	 * @return
	 */
	public static Map<String, Object> swapChildTableDataInfo(List<FieLdsModel> modelList, Map<String, Object> dataMap) {
		init();
		for (FieLdsModel swapDataVo : modelList) {
			String jnpfKey = swapDataVo.getConfig().getJnpfKey();
			String dataType = swapDataVo.getConfig().getDataType();
			String vModel = swapDataVo.getVModel();
			Boolean isMultiple = StringUtil.isEmpty(swapDataVo.getMultiple()) || swapDataVo.getMultiple().equals("false") ? false : true;
			String modelValue = String.valueOf(dataMap.get(vModel));
			if (StringUtil.isEmpty(modelValue)) {
				continue;
			}
			if (dataType != null) {
				//数据接口的数据存放
				String label;
				String value;
				String Children ="";
				List<Map<String, Object>> options = new ArrayList<>();
				if (swapDataVo.getConfig().getJnpfKey().equals(JnpfKeyConsts.CASCADER) || swapDataVo.getConfig().getJnpfKey().equals(JnpfKeyConsts.TREESELECT)) {
					isMultiple=true;
					PropsBeanModel propsBeanModel = JsonUtil.getJsonToBean(swapDataVo.getProps().getProps(), PropsBeanModel.class);
					label = propsBeanModel.getLabel();
					value = propsBeanModel.getValue();
					Children =propsBeanModel.getChildren();
				} else {
					label = swapDataVo.getConfig().getProps().getLabel();
					value = swapDataVo.getConfig().getProps().getValue();
				}
				if (dataType.equals(OnlineDataTypeEnum.STATIC.getType())) {
					if (StringUtil.isNotEmpty(swapDataVo.getOptions())) {
						options = JsonUtil.getJsonToListMap(swapDataVo.getOptions());
						 Children = swapDataVo.getProps().getPropsModel().getChildren();
						JSONArray data = JsonUtil.getListToJsonArray(options);
						OnlineDevListUtils.getOptions(label, value, Children, data, options);
					} else {
						options = JsonUtil.getJsonToListMap(swapDataVo.getSlot().getOptions());
					}
				}
				if (dataType.equals(OnlineDataTypeEnum.DYNAMIC.getType())) {
					ActionResult data = dataInterfaceService.infoToId(swapDataVo.getConfig().getPropsUrl());
					if(data.getData()!=null){
						List<Map<String, Object>> dataList =( List<Map<String, Object>>) data.getData();
						JSONArray dataAll = JsonUtil.getListToJsonArray(dataList);
						treeToList(label, value, Children, dataAll, options);
					}
				}
				if (dataType.equals(OnlineDataTypeEnum.DICTIONARY.getType())) {
					List<DictionaryDataEntity> list = dictionaryDataService.getList(swapDataVo.getConfig().getDictionaryType());
					options = list.stream().map(dic -> {
						Map<String, Object> dictionaryMap = new HashMap<>(16);
						dictionaryMap.put("id", dic.getId());
						dictionaryMap.put("fullName", dic.getFullName());
						return dictionaryMap;
					}).collect(Collectors.toList());
				}

				Map<String, String> dataInterfaceMap = new HashMap<>(16);
				options.stream().forEach(o -> {
					dataInterfaceMap.put(String.valueOf(o.get(value)), String.valueOf(o.get(label)));
				});
				if (isMultiple){
					List<String> valueList = new ArrayList<>();
					if (swapDataVo.getConfig().getJnpfKey().equals(JnpfKeyConsts.CASCADER)){
						String[][] data = JsonUtil.getJsonToBean(modelValue,String[][].class);
						for (String[] casData :data){
							for (String s : casData){
								valueList.add(s);
							}
						}
					}else {
						valueList = JsonUtil.getJsonToList(modelValue, String.class);
					}
					String allValue = valueList.stream().map(va -> dataInterfaceMap.get(va)).collect(Collectors.joining(","));
					dataMap.put(vModel, allValue);
				}else {
					dataMap.put(vModel, dataInterfaceMap.get(modelValue));
				}

			}else {
				switch (jnpfKey) {
					//公司组件
					case JnpfKeyConsts.COMSELECT:
						//部门组件
					case JnpfKeyConsts.DEPSELECT:
						//所属部门
					case JnpfKeyConsts.CURRDEPT:
						//所属组织
					case JnpfKeyConsts.CURRORGANIZE:
						if (isMultiple) {
							List<String> valueList = JsonUtil.getJsonToList(modelValue, String.class);
							String allValue = valueList.stream().map(va -> organizeService.getInfo(va).getFullName()).collect(Collectors.joining(","));
							dataMap.put(vModel, allValue);
						} else {
							dataMap.put(vModel, organizeService.getInfo(modelValue).getFullName());
						}
						break;

					//岗位组件
					case JnpfKeyConsts.POSSELECT:
						//所属岗位
					case JnpfKeyConsts.CURRPOSITION:
						if (isMultiple) {
							List<String> valueList = JsonUtil.getJsonToList(modelValue, String.class);
							String allValue = valueList.stream().map(va -> positionService.getInfo(va).getFullName()).collect(Collectors.joining(","));
							dataMap.put(vModel, allValue);
						} else {
							dataMap.put(vModel, positionService.getInfo(modelValue).getFullName());
						}
						break;

					//用户组件
					case JnpfKeyConsts.USERSELECT:
						//创建用户
					case JnpfKeyConsts.CREATEUSER:
						//修改用户
					case JnpfKeyConsts.MODIFYUSER:
						if (isMultiple && modelValue.contains("[") ) {
							List<String> valueList = JsonUtil.getJsonToList(modelValue, String.class);
							String allValue = valueList.stream().map(va -> userService.getInfo(va).getRealName()).collect(Collectors.joining(","));
							dataMap.put(vModel, allValue);
						} else {
							dataMap.put(vModel, userService.getInfo(modelValue).getRealName());
						}
						break;

					//省市区联动
					case JnpfKeyConsts.ADDRESS:
						List<ProvinceEntity> provinceEntityList = provinceService.getAllList();
						Map<String, String> provinceMap = new HashMap<>(16);
						provinceEntityList.stream().forEach(p -> provinceMap.put(p.getId(), p.getFullName()));
						break;
					case JnpfKeyConsts.RELATIONFORM:
						//取关联表单数据
						try {
						VisualdevEntity entity = visualdevService.getInfo(swapDataVo.getModelId());
						DbLinkEntity linkEntity = dblinkService.getInfo((entity.getDbLinkId()));
						@Cleanup Connection connection = VisualUtils.getDataConn(linkEntity);
						List<TableModel> tableModels = JsonUtil.getJsonToList(entity.getTables(), TableModel.class);
						Map<String, Object> formDataMap = new HashMap<>(16);
						if (tableModels.size() > 0) {
							//取关联主表
							TableModel tableModel = tableModels.stream().filter(table -> table.getTypeId().equals("1")).findFirst().orElse(null);
							String relationField = swapDataVo.getRelationField();
							String relationMainTable = tableModel.getTable();
							//取关联主键
							String pkeyName = VisualUtils.getpKey(connection, relationMainTable);
							String sql = "select " + relationField + " from " + relationMainTable + " where " + pkeyName + "='" + dataMap.get(vModel) + "'";
							formDataMap = JdbcUtil.queryOne(connection, sql);
						} else {
							VisualdevModelDataInfoVO vo = visualdevModelDataService.infoDataChange(String.valueOf(dataMap.get(vModel)), entity);
							if (vo != null) {
								formDataMap = JsonUtil.stringToMap(vo.getData());
							}
						}
						if (formDataMap.size() > 0) {
							OnlineDevListDataVO devListDataVo = new OnlineDevListDataVO();
							formDataMap = OnlinePublicUtils.mapKeyToLower(formDataMap);
							devListDataVo.setData(formDataMap);
							List<OnlineDevListDataVO> formList = new ArrayList<>();
							formList.add(devListDataVo);
							FormDataModel formDataModel = JsonUtil.getJsonToBean(entity.getFormData(), FormDataModel.class);
							List<OnlineFieldsModel> formDataVolist = JsonUtil.getJsonToList(formDataModel.getFields(), OnlineFieldsModel.class);
							List<OnlineDevListDataVO> formSwapList = OnlineDevListUtils.getSwapList(formList, formDataVolist, swapDataVo.getModelId());
							OnlineDevListDataVO dataVo = formSwapList.stream().findFirst().orElse(null);
							dataMap.put(vModel, dataVo.getData().get(swapDataVo.getRelationField().toLowerCase()));
						}
						}catch (Exception e){
							e.printStackTrace();
						}
						break;
					case JnpfKeyConsts.POPUPSELECT:
						Object data = dataInterfaceService.infoToId(swapDataVo.getInterfaceId()).getData();
						List<Map<String, Object>> mapList = (List<Map<String, Object>>) data;
						mapList.stream().filter(map -> map.get(swapDataVo.getPropsValue()).equals(dataMap.get(vModel)))
								.forEach(modelMap -> dataMap.put(vModel, modelMap.get(swapDataVo.getColumnOptions().get(0).getValue())));
						break;
					default:
						break;
				}
			}
		}
		return dataMap;
	}

	/**
	 * 转换数据格式(编辑页)
	 * @param modelList
	 * @param dataMap
	 * @return
	 */
	public static Map<String, Object> swapDataInfoType(List<FieLdsModel> modelList, Map<String, Object> dataMap) throws SQLException, ParseException, DataException, IOException {
		init();
		for (FieLdsModel swapDataVo : modelList) {
			String jnpfKey = swapDataVo.getConfig().getJnpfKey();
			String vModel = swapDataVo.getVModel();
			Object value = dataMap.get(vModel);
			if (ObjectUtil.isEmpty(value)){
				continue;
			}
			Boolean isMultiple = StringUtil.isEmpty(swapDataVo.getMultiple()) || swapDataVo.getMultiple().equals("false") ? false : true;
			switch (jnpfKey){
				case JnpfKeyConsts.ADDRESS:
				case JnpfKeyConsts.TREESELECT:
				case JnpfKeyConsts.CHECKBOX:
					isMultiple =true;
					break;
				case JnpfKeyConsts.UPLOADFZ:
				case JnpfKeyConsts.UPLOADIMG:
					List<Map<String, Object>> fileList = JsonUtil.getJsonToListMap(String.valueOf(value));
					dataMap.put(vModel,fileList);
					break;
				case JnpfKeyConsts.CASCADER:
					if (isMultiple){
						String[][] data = JsonUtil.getJsonToBean(String.valueOf(value),String[][].class);
						dataMap.put(vModel,data);
					}else {
						List<String> data = JsonUtil.getJsonToList(String.valueOf(value), String.class);
						dataMap.put(vModel,data);
					}
					break;
				default:
					break;
			}
			if (isMultiple){
				if (!jnpfKey.equals(JnpfKeyConsts.CASCADER)){
					if (String.valueOf(value).contains("[")){
						List<String> list = JsonUtil.getJsonToList(String.valueOf(value), String.class);
						dataMap.put(vModel,list);
					}else {
						dataMap.put(vModel,value);
					}
				}
			}else {
				switch (jnpfKey){
					case JnpfKeyConsts.DATE:
						//处理为时间戳
						String dateSwapInfo = swapDataVo.getFormat()!=null ? swapDataVo.getFormat() :swapDataVo.getType()!=null&& swapDataVo.getType().equals(JnpfKeyConsts.DATE)? "yyyy-MM-dd" : "yyyy-MM-dd HH:mm:ss";
						SimpleDateFormat sdf = new SimpleDateFormat(dateSwapInfo);
						String s1 = String.valueOf(value);
						Long s = null;
						try {
							s = sdf.parse(s1).getTime();
						} catch (ParseException e) {
							e.printStackTrace();
						}
						dataMap.put(vModel,s);
						break;
					case JnpfKeyConsts.SWITCH:
					case JnpfKeyConsts.SLIDER:
						dataMap.put(vModel,value!=null ? Integer.parseInt(String.valueOf(value)):null);
						break;

					case JnpfKeyConsts.CURRORGANIZE:
					case JnpfKeyConsts.CURRDEPT:
						dataMap.put(vModel,organizeService.getInfo(String.valueOf(value)).getFullName());
						break;
					case JnpfKeyConsts.CURRPOSITION:
						dataMap.put(vModel,positionService.getInfo(String.valueOf(value)).getFullName());
						break;
					case JnpfKeyConsts.CREATEUSER:
					case JnpfKeyConsts.MODIFYUSER:
						String userValue = String.valueOf(value).equalsIgnoreCase("admin")
								? "管理员"
								: userService.getInfo(String.valueOf(value)).getRealName();
						dataMap.put(vModel,userValue);
						break;
					case JnpfKeyConsts.RELATIONFORM:
						//取关联表单数据
						VisualdevEntity entity = visualdevService.getInfo(swapDataVo.getModelId());
						DbLinkEntity linkEntity = dblinkService.getInfo((entity.getDbLinkId()));
						@Cleanup Connection connection = VisualUtils.getDataConn(linkEntity);
						List<TableModel> tableModels = JsonUtil.getJsonToList(entity.getTables(), TableModel.class);
						Map<String, Object> formDataMap = new HashMap<>(16);
						if (tableModels.size() > 0) {
							//取关联主表
							TableModel tableModel = tableModels.stream().filter(table -> table.getTypeId().equals("1")).findFirst().orElse(null);
							String relationField = swapDataVo.getRelationField();
							String relationMainTable = tableModel.getTable();
							//取关联主键
							String pkeyName = VisualUtils.getpKey(connection, relationMainTable);
							String sql = "select " + relationField + " from " + relationMainTable + " where " + pkeyName + "='" + dataMap.get(vModel) + "'";
							try {
								formDataMap = JdbcUtil.queryOne(connection, sql);
							} catch (DataException e) {
								e.printStackTrace();
							}
						} else {
							VisualdevModelDataInfoVO vo = visualdevModelDataService.infoDataChange(String.valueOf(dataMap.get(vModel)), entity);
							if (vo != null) {
								formDataMap = JsonUtil.stringToMap(vo.getData());
							}
						}
						if (formDataMap.size() > 0) {
							OnlineDevListDataVO devListDataVo = new OnlineDevListDataVO();
							formDataMap = OnlinePublicUtils.mapKeyToLower(formDataMap);
							devListDataVo.setData(formDataMap);
							List<OnlineDevListDataVO> formList = new ArrayList<>();
							formList.add(devListDataVo);

							FormDataModel formDataModel = JsonUtil.getJsonToBean(entity.getFormData(), FormDataModel.class);
							List<OnlineFieldsModel> formDataVolist = JsonUtil.getJsonToList(formDataModel.getFields(), OnlineFieldsModel.class);
							List<OnlineDevListDataVO> formSwapList = OnlineDevListUtils.getSwapList(formList, formDataVolist, swapDataVo.getModelId());
							OnlineDevListDataVO dataVo = formSwapList.stream().findFirst().orElse(null);
							dataMap.put(vModel, dataVo.getData().get(swapDataVo.getRelationField().toLowerCase()));
						}
						break;
					case JnpfKeyConsts.POPUPSELECT:
						Object data = dataInterfaceService.infoToId(swapDataVo.getInterfaceId()).getData();
						List<Map<String, Object>> mapList = (List<Map<String, Object>>) data;
						mapList.stream().filter(map -> map.get(swapDataVo.getPropsValue()).equals(dataMap.get(vModel)))
								.forEach(modelMap -> dataMap.put(vModel, modelMap.get(swapDataVo.getColumnOptions().get(0).getValue())));
						break;
					default:
						break;
				}
			}
		}
		return dataMap;
	}
	private static void treeToList(String value, String label, String children, JSONArray data, List<Map<String, Object>> result) {
		for (int i = 0; i < data.size(); i++) {
			JSONObject ob = data.getJSONObject(i);
			Map<String, Object> tree = new HashMap<>(16);
			tree.put(value, String.valueOf(ob.get(value)));
			tree.put(label, String.valueOf(ob.get(label)));
			result.add(tree);
			if (ob.get(children) != null) {
				JSONArray childArray = ob.getJSONArray(children);
				treeToList(value, label, children, childArray, result);
			}
		}
	}
}